package com.liaoban.app.ui.base;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.ui.common.EmptyView;
import com.liaoban.app.util.CommonUtils;


import java.util.List;

import butterknife.BindView;

/**
 * Created by ChenYuYun on 2018/3/26.
 * 标准列表的RecyclerView页面
 */
public abstract class BaseListFragment<T> extends BaseFragment {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_layout)
    protected SwipeRefreshLayout swipeLayout;
    private BaseQuickAdapter mAdapter;
    private int mPageIndex = 1;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_list;
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    @Override
    protected void initView(View contentView) {
        swipeLayout.setOnRefreshListener(this::refresh);
        initRecyclerView();
        refresh();
    }


    protected void setRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rvList.setLayoutManager(manager);
    }

    private void initRecyclerView() {
        setRecyclerView(rvList);
        mAdapter = getAdapter();
        rvList.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this::loadData, rvList);
        mAdapter.disableLoadMoreIfNotFullPage();
        mAdapter.setEmptyView(getEmptyView());
        mAdapter.setOnItemClickListener(this::itemClick);
        mAdapter.setOnItemChildClickListener(this::itemChildClick);
    }

    //返回空控制
    protected View getEmptyView() {
        return new EmptyView(getActivity());
    }

    private void loadData() {
        swipeLayout.setEnabled(false);
        getData();
    }

    protected void refresh() {
        mPageIndex = 1;
        loadData();
    }

    public abstract BaseQuickAdapter getAdapter();

    public abstract void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position);

    public abstract void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position);

    public abstract void getData();


    //数据请求成功后的结果处理
    public void doSuc(List<T> rows, int totalPageCount) {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
            swipeLayout.setEnabled(true);
        }
        mAdapter.loadMoreComplete();
        //if (!CommonUtils.isEmpty(rows)) {
            if (mPageIndex == 1) {
                mAdapter.setNewData(rows);
            } else {
                mAdapter.addData(rows);
            }
        //}
        if (mPageIndex++ >= totalPageCount) {
            mAdapter.loadMoreEnd(false);
        }
    }

    //数据请求后失败处理
    protected void doError(Throwable throwable) {
        swipeLayout.setEnabled(true);
        swipeLayout.setRefreshing(false);
        mAdapter.loadMoreFail();
        defOnError(throwable);
    }


}
